package com.main.hotel.controller;

import com.main.hotel.entity.OrderInfo;
import com.main.hotel.service.OrderInfoService;
import com.main.hotel.util.HandleFile;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
  @RequestMapping("/handleFile")
  @Slf4j
  public class HandleFileController{
    @Autowired
    private OrderInfoService orderInfoService;
    /**
     * excel生成下载
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/createExcel")
    public String createExcel(String order_id,HttpServletResponse response) throws Exception{
      Map<String,Object> excelMap = new HashMap<>();
      //1.设置Excel表头
      List<String> headerList = new ArrayList<>();
      headerList.add("订单号");
      headerList.add("客户姓名");
      headerList.add("客房类型");
      headerList.add("交易金额");
      headerList.add("下单时间");
      headerList.add("入住时间");
      headerList.add("离店时间");
      headerList.add("酒店名称");
      excelMap.put("header",headerList);

      //2.是否需要生成序号，序号从1开始(true-生成序号 false-不生成序)
      boolean isSerial = false;
      excelMap.put("isSerial",isSerial);

      //3.sheet名
      String sheetName = "订单信息";
      excelMap.put("sheetName",sheetName);


      //4.需要放入Excel中的数据
      Map<String,Object> map = new HashMap<>();

OrderInfo orderInfo1=orderInfoService.getOrderById(order_id);
List<OrderInfo> list2=new ArrayList<>();
list2.add(orderInfo1);
      List<OrderInfo> rows = list2;

      List<List<String>> data= new ArrayList<>();

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      for (OrderInfo orderInfo:rows){
        //所有的数据顺序必须和表头一一对应
        //list存放每一行的数据（让所有的数据类型都转换成String，这样就无需担心Excel表格中数据不对）
        List<String> list = new ArrayList<>();
        list.add(String.valueOf(orderInfo.getOrder_id()));
        list.add(String.valueOf(orderInfo.getName()));
        list.add(String.valueOf(orderInfo.getRoomType()));
        list.add(String.valueOf(orderInfo.getOrder_cost()));
        list.add(String.valueOf(orderInfo.getCreateTime()));
        list.add(String.valueOf(orderInfo.getOrder_date()));
        list.add(String.valueOf(orderInfo.getOrder_date()));
        list.add(String.valueOf("松湖酒店"));
        //data存放全部的数据
        data.add(list);
      }
      excelMap.put("data",data);

      //Excel文件内容设置
      HSSFWorkbook workbook = HandleFile.createExcel(excelMap);

      String fileName = "我的订单.xls";

      //生成excel文件
      HandleFile.buildExcelFile(fileName, workbook);

      //浏览器下载excel
      HandleFile.buildExcelDocument(fileName,workbook,response);

      return "down excel";

    }
  @RequestMapping(value = "/createExcelList")
  public String createExcelList(int customer_id,String time,HttpServletResponse response) throws Exception{
    Map<String,Object> excelMap = new HashMap<>();
    //1.设置Excel表头
    List<String> headerList = new ArrayList<>();
    headerList.add("订单号");
    headerList.add("客户姓名");
    headerList.add("客房类型");
    headerList.add("交易金额");
    headerList.add("下单时间");
    headerList.add("入住时间");
    headerList.add("离店时间");
    headerList.add("酒店名称");
    excelMap.put("header",headerList);

    //2.是否需要生成序号，序号从1开始(true-生成序号 false-不生成序)
    boolean isSerial = false;
    excelMap.put("isSerial",isSerial);

    //3.sheet名
    String sheetName = "订单信息";
    excelMap.put("sheetName",sheetName);


    //4.需要放入Excel中的数据
    Map<String,Object> map = new HashMap<>();

    List<OrderInfo> rows=orderInfoService.getOrderByTime(customer_id,time);

    List<List<String>> data= new ArrayList<>();

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    for (OrderInfo orderInfo:rows){
      //所有的数据顺序必须和表头一一对应
      //list存放每一行的数据（让所有的数据类型都转换成String，这样就无需担心Excel表格中数据不对）
      List<String> list = new ArrayList<>();
      list.add(String.valueOf(orderInfo.getOrder_id()));
      list.add(String.valueOf(orderInfo.getName()));
      list.add(String.valueOf(orderInfo.getRoomType()));
      list.add(String.valueOf(orderInfo.getOrder_cost()));
      list.add(String.valueOf(orderInfo.getCreateTime()));
      list.add(String.valueOf(orderInfo.getOrder_date()));
      list.add(String.valueOf(orderInfo.getOrder_date()));
      list.add(String.valueOf("松湖酒店"));
      //data存放全部的数据
      data.add(list);
    }
    excelMap.put("data",data);

    //Excel文件内容设置
    HSSFWorkbook workbook = HandleFile.createExcel(excelMap);

    String fileName = "我的订单.xls";

    //生成excel文件
    HandleFile.buildExcelFile(fileName, workbook);

    //浏览器下载excel
    HandleFile.buildExcelDocument(fileName,workbook,response);

    return "down excel";

  }
}
